package org.jetlinks.demo.protocol.siterwell.tcp.message;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.jetlinks.demo.protocol.siterwell.tcp.constant.DeviceType;
import org.jetlinks.demo.protocol.siterwell.tcp.util.ByteUtils;

import java.util.Optional;

/**
 * 设备报警,一般是子设备报警
 * 设备网关发往平台的数据
 */
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class D2SDeviceAlarm extends AbstractCommandRequest {

    private String childDeviceId;
    private String roomNo;
    private DeviceType childDeviceType;

    public static D2SDeviceAlarm of(byte[] data) {
        D2SDeviceAlarm message = new D2SDeviceAlarm();
        message.fromBytes(data, 0);
        return message;
    }


    @Override
    protected void parseData() {
        String hexDataSource = this.getHexDataSource();
        this.childDeviceId = ByteUtils.subHexStrByByte(hexDataSource, 0, 3);
        this.roomNo = ByteUtils.subHexStrByByte(hexDataSource, 3, 5);
        Optional<DeviceType> deviceType = DeviceType.of(this.getHexChildDeviceType());
        if (deviceType.isPresent()) {
            this.childDeviceType = deviceType.get();
        }
    }


    @Override
    public String toString() {
        return "{ device alarm : " +
                "childDeviceId=" + childDeviceId +
                ", roomNo=" + roomNo +
                '}';
    }
}
